package net.gz01.wallet.web.controller.admin;

import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;

import net.gz01.util.ZODate;
import net.gz01.util.ZOMD5;
import net.gz01.wallet.common.PageRespon;
import net.gz01.wallet.web.controller.BaseController;
import net.gz01.wallet.web.model.AdminRole;
import net.gz01.wallet.web.model.AdminUser;
import net.gz01.wallet.web.model.UserSettings;

public class AdminUserController extends BaseController{

	public void index(){
		setAttr("sidebar", getSession().getAttribute("sidebar"));
		render("index.html");
	}

	public void search(){
		Page<AdminUser> items = AdminUser.dao.search(super.getSearchCondition());
		setPageResultToRequest("items", items);
		render("system/adminUserList.html");
	}

	public void listJson(){
		Page<AdminUser> pageResult = AdminUser.dao.search(super.getSearchCondition());
		renderJson(new PageRespon(pageResult));
	}

	public void addPage(){
		setAttr("roleList", AdminRole.dao.selectAll());
		render("system/adminUserAdd.html");
	}

	public void updatePage(){
		AdminUser user = AdminUser.dao.findById(getPara("id"));
		setAttr("roleList", AdminRole.dao.selectAll());
		setAttr("adminUser", user);
		render("system/adminUserUpdate.html");
	}

	public void add(){
		AdminUser user = getModel(AdminUser.class);
		if(AdminUser.dao.selectByUserName(user.getUserName()) != null){
			renderFail("用户名已存在");
		}else {
			user.setPassword(ZOMD5.codeFor(user.getPassword()));
			user.setUpdateTime(ZODate.getDateTime());
			user.setEnable(true);
			user.save();
			renderSuccess();
		}
	}

	public void changeEnable(){
		Integer userId = getParaToInt("id");
		AdminUser.dao.changeEnable(userId);
		renderSuccess(false);
	}

	public void update(){
		AdminUser user = getModel(AdminUser.class,true);
		String newPassword = getPara("newPassword");
		if(StrKit.notBlank(newPassword)){
			user.setPassword(ZOMD5.codeFor(newPassword));
		}
		user.setUpdateTime(ZODate.getDateTime());
		user.update();
		renderSuccess();
	}

	public void delete(){
		AdminUser.dao.deleteById(getPara("id"));
		renderSuccess(false);
	}

	public void changePasswordPage(){
		render("changePassword.html");
	}

	public void changePassword(){
		String userId = getPara("userId");
		String oldPassword = getPara("oldPassword");
		String newPassword = getPara("newPassword");
		AdminUser user = AdminUser.dao.findById(userId);
		if(user.getPassword().equalsIgnoreCase(ZOMD5.codeFor(oldPassword))){
			user.setPassword(ZOMD5.codeFor(newPassword));
			user.update();
			renderSuccess();
		}else{
			renderFail("旧密码不正确");
		}
	}

	public void settings() {
		if ("GET".equalsIgnoreCase(getRequest().getMethod())) {
			String site = getPara("site");
			@SuppressWarnings("unchecked")
			Map<String, UserSettings> map = (Map<String, UserSettings>)getSession().getAttribute("UserSettings");
			renderText(map.get(site).getSettings(), "application/json;charset=utf-8");
		} else {
			JSONObject json = JSON.parseObject(HttpKit.readData(getRequest()));
			// 删除旧的
			AdminUser user = (AdminUser)getSession().getAttribute("loginAdminUser");
			UserSettings.dao.deleteByUser(user.getId(), json.getString("site"));
			// 保存新的
			UserSettings settings = new UserSettings();
			settings.setUserId(user.getId());
			settings.setUserName(user.getUserName());
			settings.setUserType("0");
			settings.setFuncKey(json.getString("site"));
			settings.setSettings(json.getJSONArray("fields").toJSONString());
			settings.save();
			// 更新session中的缓存
			@SuppressWarnings("unchecked")
			Map<String, UserSettings> map = (Map<String, UserSettings>)getSession().getAttribute("UserSettings");
			map.put(json.getString("site"), settings);
			getSession().setAttribute("UserSettings", map);
			renderSuccess();
		}
	}
}
